package editor.manager;

import java.awt.Color;

import javax.swing.JEditorPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;

import editor.Context;
import editor.Mode;
import editor.project.ProjectFile;

/**
 * Manages the console 
 * 
 * @author Mark Dessain
 */
@SuppressWarnings("serial")
public class ConsoleManager extends JTabbedPane implements HyperlinkListener, Manager
{
	
	private JEditorPane javaConsole = new JEditorPane();
	private JEditorPane jwalkQuestion = new JEditorPane();
	private JPanel jwalkPanel = new JPanel();
	
	/**
	 * Starts Console Manager
	 */
	public ConsoleManager()
	{		
		javaConsole.setEditorKit(JEditorPane.createEditorKitForContentType("text/html"));
		javaConsole.setEditable(false);
		jwalkQuestion.setOpaque(false);
		javaConsole.addHyperlinkListener(this);
		
		jwalkQuestion.setEditorKit(JEditorPane.createEditorKitForContentType("text/html"));
		jwalkQuestion.setEditable(false);
		jwalkQuestion.setOpaque(false);
		jwalkQuestion.addHyperlinkListener(this);
		
		codingMode();
		
		this.setBackground(Color.WHITE);

		javaConsole.setOpaque(false);
		jwalkPanel.setOpaque(false);
	}

	/**
	 * Clears the console panel
	 */
	public void clearJwalkPanel()
	{
		jwalkPanel.removeAll();;
	}
	
	/**
	 * @return the console panel
	 */
	public JPanel getJwalkPanel()
	{
		return jwalkPanel;
	}
	
	/**
	 * @return the question panel
	 */
	public JEditorPane getJwalkQuestion()
	{
		return jwalkQuestion;
	}
	
	/**
	 * @return the console
	 */
	public JEditorPane getJavaConsole()
	{
		return javaConsole;
	}

	/** 
	 * Adds links between console and text-area
	 * 
	 * (non-Javadoc)
	 * @see javax.swing.event.HyperlinkListener#hyperlinkUpdate(javax.swing.event.HyperlinkEvent)
	 */
	@Override
	public void hyperlinkUpdate(HyperlinkEvent evt) 
	{
        if (evt.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
        	
            String[] error = evt.getDescription().split(":");
            
            if(error.length <= 3)
            {
	            if(error.length == 3)
	            {
	            	error[0] = error[0] + ":" + error[1];
	            	error[1] = error[2];
	            }
	            		            
	            ProjectFile file = Context.getMainProject().findFile(Context.getMainProject().getSrc(), error[0]);
	            WorkSpaceManager.textAreaManager.add(file, false);          
	      
            	WorkSpaceManager.textAreaManager.selectLine(WorkSpaceManager.textAreaManager.getSelectedEditor(), Integer.parseInt(error[1]));
	        }

        }		
	}


	/* (non-Javadoc)
	 * @see editor.manager.Manager#switchMode(editor.Mode)
	 */
	@Override 
	public void switchMode(Mode mode)
	{
		if(mode == Mode.CODING)
		{
			codingMode();
		}
		else if(mode == Mode.QUESTION)
		{
			questionMode();	
		}
		else if(mode == Mode.TESTING)
		{
			testingMode();
		}
	}
	
	/* (non-Javadoc)
	 * @see editor.manager.Manager#codingMode()
	 */
	@Override
	public void codingMode()
	{
		removeAll();
		addTab("Java Console", new JScrollPane(javaConsole));	
	}
	
	/* (non-Javadoc)
	 * @see editor.manager.Manager#questionMode()
	 */
	@Override
	public void questionMode()
	{
		removeAll();
		addTab("JWalk Questions", new JScrollPane(jwalkPanel));	
	}

	/* (non-Javadoc)
	 * @see editor.manager.Manager#testingMode()
	 */
	@Override
	public void testingMode() 
	{
		removeAll();
		addTab("Java Console", new JScrollPane(javaConsole));	
	}	
}